﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 内切角颜色混合，作迷彩色字体
    /// </summary>
    [ExecuteInEditMode]
    [DisallowMultipleComponent]
    [RequireComponent(typeof(UIMaterialEffect))]
    [AddComponentMenu("Nirvana/UI/Effects/Inner Bevel")]
    public class UIInnerBevel : BaseMeshEffect
    {
        [SerializeField]
        private Color highlightColor = Color.white;
        [SerializeField]
        private ColorMode highlightColorMode;
        [SerializeField]
        private Color shadowColor = Color.black;
        [SerializeField]
        private ColorMode shadowColorMode;
        [SerializeField]
        private Vector2 bevelDirectionAndDepth = Vector2.one;
        private UIMaterialEffect _uimateffect;

        protected override void Awake()
        {
            this.ModifyMaterials();
        }

        protected override void OnEnable()
        {
            this.ModifyMaterials();
        }

        protected override void OnDisable()
        {
            this.ModifyMaterials();
        }

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            ModifyMaterials();
            if (base.graphic != null) base.graphic.SetVerticesDirty();
        }
#endif

        public override void ModifyMesh(VertexHelper vh)
        {
            if (!this.IsActive() || vh.currentVertCount <= 0)
            {
                return;
            }

            List<UIVertex> tempList = VertexLists.Get();
            vh.GetUIVertexStream(tempList);
            List<UIVertex> newlist = ModifyVertex(tempList);
            if (newlist != null)
            {
                vh.Clear();
                vh.AddUIVertexTriangleStream(newlist);
            }
            VertexLists.Set(newlist);
        }

        private void ModifyMaterials()
        {
#if UNITY_EDITOR
            PrefabType prefabType = PrefabUtility.GetPrefabType(gameObject);
            if (prefabType == PrefabType.Prefab) return;
            if (_uimateffect == null) _uimateffect = this.GetOrAddComponent<UIMaterialEffect>();

            var newmat = _uimateffect.MaterialsEffect;
            newmat.isOpenInnerBevel = this.enabled;
            newmat.highlightColorMode = this.highlightColorMode;
            newmat.highlightColor = this.highlightColor;
            newmat.shadowColor = this.shadowColor;
            newmat.shadowColorMode = this.shadowColorMode;
            newmat.highlightOffset = this.bevelDirectionAndDepth / 500f;

            _uimateffect.MaterialsEffect = newmat;
            _uimateffect.Save();
#endif
        }

        private List<UIVertex> ModifyVertex(List<UIVertex> _list)
        {
            int count = _list.Count;
            for(int i = 0; i < count; i += 6)
            {
                UIVertex vertex = _list[i];
                Vector2 normalized = (_list[i + 1].uv0 - _list[i].uv0).normalized;
                Vector2 normalized2 = (_list[i + 1].uv0 - _list[i + 2].uv0).normalized;
                Vector4 tangent = normalized;
                tangent.z = normalized2.x;
                tangent.w = normalized2.y;
                vertex.tangent = tangent;

                if (vertex.uv1 == Vector4.zero) vertex.uv1 = new Vector2(1, 1);
                _list[i] = vertex;
                for(int j = 1; j < 6; j++)
                {
                    vertex = _list[i + j];
                    vertex.tangent = _list[i].tangent;
                    if (vertex.uv1 == Vector4.zero) vertex.uv1 = new Vector2(1, 1);
                    _list[i + j] = vertex;
                }
            }

            return _list;
        }









    }
}

